package lesson02;

import javafx.scene.chart.ScatterChart;

import java.io.*;
import java.util.Scanner;

/**
 * 功能描述
 *
 * @author wileda
 * @date 2023/02/13  10:14
 */
public class Demo08_Copy {
    public static void main(String[] args) throws IOException {
        /**
        源文件路径以及校验
         */
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入源文件：");
        String sourcePath = scanner.nextLine();
        File sourceFile = new File(sourcePath);
        if(!sourceFile.isFile()){
            System.out.println("输入的不是一个有效地址，请重新输入");
            return;
        }
        /**
         * 目标路径及校验
         */
        System.out.println("请输入目标路径");
        String destPath = scanner.nextLine();
        File destFile = new File(destPath);
        if(destFile.exists()){
            System.out.println("输入的目标路径有误，请重新输入");
            return;
        }
        if(!destFile.getParentFile().exists()){
            System.out.println("目标文件的目录不正确");
            return;
        }
        /**
         * 输入输出流
         */
        try(InputStream inputStream = new FileInputStream(sourceFile);
        OutputStream outputStream = new FileOutputStream(destFile);) {
            byte[] bytes = new byte[1024];
            while (true) {
                int len = inputStream.read(bytes);
                if (len == -1) {
                    break;
                }
                outputStream.write(bytes, 0, len);
                outputStream.flush();
                System.out.println("复制成功：" + sourceFile.getCanonicalPath() + " ---> " + destFile.getCanonicalPath());
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
